<?php
require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'wsr.inc.php';
require_once WSR_DIR . DIRECTORY_SEPARATOR . 'tests/supercompare.inc.php';
require_once WSR_DIR . 'axgraph.class.php';
require_once WSR_DIR . 'activation.class.php';

class ActivationTest extends PHPUnit_Framework_TestCase {
	
	function makeTestGraph() {
		return array( 'Nad' => array( 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type' 
									         => array( array( 'type' => 'uri', 'value' => 'http://www.example.com/Employee' ) ),
									  'http://www.example.com/Employed_in' 
									         => array( array( 'type' => 'uri', 'value' => 'http://www.talis.com/' ) )
									),
					  'Alan' => array( 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type' 
									         => array( array( 'type' => 'uri', 'value' => 'http://www.example.com/Employee' ) ),
									   'http://www.example.com/Employed_in' 
									         => array( array( 'type' => 'uri', 'value' => 'http://www.comp.lancs.ac.uk/' ) ),
									   'http://xmlns.com/foaf/0.1/Friend_of'
									         => array( array( 'type' => 'uri', 'value' => 'Nad' ) ),
									)
					   );
	}
	
	function test_set_prams() {
		$params = new ActivationParams();
		$params->load_threshold = 3;
		$params->spread_factor = 0.2;
		$activation = new Activation($params);
		$gotparams = $activation->get_params();
		$this->assertEquals( $gotparams, $params, "initialisation params");
		$params->spread_factor = 0.3;
		$activation->set_params($params);
		$gotparams = $activation->get_params();
		$this->assertEquals( $gotparams, $params, "set params");
	}

	function test_activation_one_step() {
		$params = new ActivationParams();
		$params->load_threshold = 3;
		$params->spread_factor = 0.2;
		$activation = new Activation($params);
		$graph = $this->makeTestGraph();
		$axgraph = new AxGraph($graph);
		$activation->set_graph($axgraph);
		
		$initial_entites = array('Nad');
		$activation->set_initial_activated_entities( $initial_entites );
		$activation->do_steps(1);
		
		$delta = 0.01;

		$in_ex    = array( 'Nad' => 100, 'http://www.example.com/Employee' => 20, 'http://www.talis.com/' => 20, 'Alan' => 20 );
		$act_exp  = array( 'Nad' => 100, 'http://www.example.com/Employee' =>  5, 'http://www.talis.com/' =>  5, 'Alan' =>  5 );

		$this->assertEquals( $activation->in, $in_ex, "incoming", $delta );
		$this->assertEquals( $activation->activation_levels, $act_exp, "activation", $delta );
	}

	function test_activation_one_step_multiple_initial() {
		$params = new ActivationParams();
		$params->load_threshold = 3;
		$params->spread_factor = 0.2;
		$activation = new Activation($params);
		$graph = $this->makeTestGraph();
		$axgraph = new AxGraph($graph);
		$activation->set_graph($axgraph);
		
		$initial_entites = array('Nad','http://www.comp.lancs.ac.uk/');
		$activation->set_initial_activated_entities( $initial_entites, 70 );
		$activation->do_steps(1);
		
		$delta = 0.01;

		$in_ex    = array( 'Nad' => 70, 'http://www.comp.lancs.ac.uk/' => 70,
						   'http://www.example.com/Employee' => 14,   'http://www.talis.com/' => 14,   'Alan' => 28 );
		$act_exp  = array( 'Nad' => 88, 'http://www.comp.lancs.ac.uk/' => 88,
						   'http://www.example.com/Employee' =>  3.2, 'http://www.talis.com/' =>  3.2, 'Alan' => 10.6 );

		$this->assertEquals( $activation->in, $in_ex, "incoming Nad and Lancs initial", $delta );
		$this->assertEquals( $activation->activation_levels, $act_exp, "activation Nad and Lancs initial", $delta );
	}

	function test_activation_multi_step() {
		$params = new ActivationParams();
		$params->load_threshold = 3;
		$params->spread_factor = 0.2;
		$activation = new Activation($params);
		$graph = $this->makeTestGraph();
		$axgraph = new AxGraph($graph);
		$activation->set_graph($axgraph);
		
		$initial_entites = array('Nad');
		$activation->set_initial_activated_entities( $initial_entites );
		$activation->do_steps(5);
		
		$delta = 0.01;

		$in_ex    = array( 'Nad' => 102.662456, 'http://www.example.com/Employee' => 21.086478, 'http://www.talis.com/' => 20,
						   'http://www.comp.lancs.ac.uk/' => 1.086478, 'Alan' => 20.619394 );
		$act_exp  = array( 'Nad' => 100, 'http://www.example.com/Employee' =>  5.7605346, 'http://www.talis.com/' =>  5,
						   'http://www.comp.lancs.ac.uk/' => 0.2172956, 'Alan' =>  5.4335758 );
		
		$this->assertEquals( $activation->in, $in_ex, "multi-incoming", $delta );
		$this->assertEquals( $activation->activation_levels, $act_exp, "multi-activation", $delta );
	}
	


	function test_activation_flood() {
		$params = new ActivationParams();
		$params->load_threshold = 3;
		$params->spread_factor = 0.7;
		$activation = new Activation($params);
		$graph = $this->makeTestGraph();
		$axgraph = new AxGraph($graph);
		$activation->set_graph($axgraph);
		
		$initial_entites = array('Nad');
		$activation->set_initial_activated_entities( $initial_entites );
		$activation->do_steps(20);
		
		$delta = 0.01;

		$act_exp  = array( 'Nad' => 100, 'http://www.example.com/Employee' =>  100, 'http://www.talis.com/' =>  88,
						   'http://www.comp.lancs.ac.uk/' => 88, 'Alan' =>  100 );
		
		//print_r($activation->activation_levels);
		
		$this->assertEquals( $activation->activation_levels, $act_exp, "flood using large spread factor", $delta );
	}


	function test_activation_feedback_control() {
		$params = new ActivationParams();
		$params->load_threshold = 3;
		$params->spread_factor = 0.7;
		$params->use_feedback_control = true;
		
		$activation = new Activation($params);
		$graph = $this->makeTestGraph();
		$axgraph = new AxGraph($graph);
		$activation->set_graph($axgraph);
		
		$initial_entites = array('Nad');
		$activation->set_initial_activated_entities( $initial_entites );
		$activation->do_steps(3);
		
		$delta = 0.01;

		$act_exp  = array( 'Nad' => 100, 'http://www.example.com/Employee' =>  88, 'http://www.talis.com/' =>  88,
						   'http://www.comp.lancs.ac.uk/' => 75.4, 'Alan' =>  88 );
		
		//print_r($activation->activation_levels);
		
		$this->assertEquals( $activation->activation_levels, $act_exp, "feedback control", $delta );
	}

	function test_activation_meta() {
		$params = new ActivationParams();
		$params->load_threshold = 3;
		$params->spread_factor = 0.4;
		$params->use_feedback_control = true;
		
		$meta = array (
					   'fan-out'=>
						   array(	'Nad'  => array(	'http://www.w3.org/1999/02/22-rdf-syntax-ns#type' => 1,
														'http://www.example.com/Employed_in'  => 1
												),
									'Alan' => array(	'http://www.w3.org/1999/02/22-rdf-syntax-ns#type' => 1,
														'http://www.example.com/Employed_in'   => 1,
														'http://xmlns.com/foaf/0.1/Friend_of'  => 10
													)
							   ),
					   'fan-in'=>
						   array(	'Nad'  => array(	'http://www.w3.org/1999/02/22-rdf-syntax-ns#type' => 1000,
														'http://xmlns.com/foaf/0.1/Friend_of'  => 20
												),
									'Alan' => array(	'http://www.w3.org/1999/02/22-rdf-syntax-ns#type' => 1000,
														'http://www.example.com/Employed_in'   => 100,
														'http://xmlns.com/foaf/0.1/Friend_of'  => 10
													)
							   )
					   );

		$activation = new Activation($params);
		$graph = $this->makeTestGraph();
		$axgraph = new AxGraph($graph);
		$activation->set_graph($axgraph);
		$activation->set_meta($meta);
		//$activation->debug = true;
		
		$initial_entites = array('Alan');
		$activation->set_initial_activated_entities( $initial_entites );
		$activation->do_steps(3);
		
		$delta = 0.01;

		$act_exp  = array( 'Alan' =>  100, 'http://www.talis.com/' =>  0.1504, 'http://www.example.com/Employee' =>  27,
						   'http://www.comp.lancs.ac.uk/' => 27, 'Nad' => 1.88);
		
		//print_r($activation->activation_levels);
		
		$this->assertEquals( $activation->activation_levels, $act_exp, "using per entity meta information", $delta );
	}
	

	function test_activation_meta_avg() {
		$params = new ActivationParams();
		$params->load_threshold = 3;
		$params->spread_factor = 0.4;
		$params->use_feedback_control = true;
		
		$meta_avg = array (
					   'avg-fan-out'=>
						   array(	'http://www.w3.org/1999/02/22-rdf-syntax-ns#type' => 1,
									'http://www.example.com/Employed_in'  => 1,
									'http://xmlns.com/foaf/0.1/Friend_of'  => 15
							   ),
					   'avg-fan-in'=>
						   array(	'http://www.w3.org/1999/02/22-rdf-syntax-ns#type' => 1000,
									'http://www.example.com/Employed_in'   => 100,
									'http://xmlns.com/foaf/0.1/Friend_of'  => 15
							   )
					   );

		$activation = new Activation($params);
		$graph = $this->makeTestGraph();
		$axgraph = new AxGraph($graph);
		$activation->set_graph($axgraph);
		$activation->set_meta($meta_avg);
		//$activation->debug = true;
		
		$initial_entites = array('Alan');
		$activation->set_initial_activated_entities( $initial_entites );
		$activation->do_steps(3);
		
		$delta = 0.01;

		$act_exp  = array( 'Alan' =>  100, 'http://www.talis.com/' =>  0.0428394666667, 'http://www.example.com/Employee' =>  27,
						   'http://www.comp.lancs.ac.uk/' => 27, 'Nad' => 0.535493333333);
		
		//print_r($activation->activation_levels);
		
		$this->assertEquals( $activation->activation_levels, $act_exp, "using average meta information", $delta );
	}


}


?>